package com.huobao.yunying.activitys;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_baitiao;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.Baitiao;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.yunying.utils.OpenBrowUtils;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
/**
 * 白条
 * @author Administrator
 *
 */
public class Act_Baitiao extends HuobaoBaseActivity implements OnItemClickListener{

	@ViewInject(R.id.imageview)
	private ImageView imageview;
	@ViewInject(R.id.webview)
	private WebView webview;
	@ViewInject(R.id.listview)
	private NoScrollListView listview;
	private Adapter_baitiao adapter;
	private String user_id;
	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_baitiao;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "掌婴白条";
	}

	@Override
	protected void init() {
		user_id = getIntent().getStringExtra("id");
		adapter = new Adapter_baitiao(context);
		listview.setOnItemClickListener(this);
		listview.setAdapter(adapter);
		getData();
	}

	
	private void getData(){
		Map<String, String> map = HttpUtil.getParams("app","baitiao");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				String banber = jsonObject.getString("banber");
				String s_content = jsonObject.getString("s_content");
				List<Baitiao> list = JSON.parseArray(jsonObject.getString("common_problem"), Baitiao.class);
				adapter.addAll(list);
				ImageUtils.loadimg(imageview, banber, new OnImageLoadListener() {
					
					@Override
					public void onLoadSuccess(Drawable drawable) {
						int newWidth = ScreenUtil.getInstant().getDisWidth(context);
						int newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
						LayoutParams layoutParams = (LayoutParams) imageview.getLayoutParams();
						layoutParams.width = newWidth;
						layoutParams.height = newHight;
						imageview.setLayoutParams(layoutParams);
					}
				});
				webview.loadDataWithBaseURL(null, s_content, "text/html", "utf-8", null);
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		OpenBrowUtils.startBrower(context, adapter.getItem(position).vier_url);
	}
	
	public void onBaitiaoClick(View v){
		Intent intent = new Intent(this, Act_baitiao_shenqing.class);
		intent.putExtra("id", user_id);
		startActivityForResult(intent, 1);
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if(arg0 == 1 && arg1 == RESULT_OK){
			finish();
		}
		super.onActivityResult(arg0, arg1, arg2);
	}
}
